import { type RouteRecordRaw, createRouter, createWebHistory } from 'vue-router';
const Layouts = () => import('@/layout/index.vue');
//菜单路由
import { LayoutsRouter } from './Layouts'
/**
 * 路由守卫
 */
import { setupAuthGuard } from './authGuard'
/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
 
  {
    path:'/',
    redirect:"/layout"
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      hidden: true,  
      // requiresAuth:true,
    },
  },
  {
    path: '/403',
    component: () => import('@/views/err_page/403.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    component: () => import('@/views/err_page/404.vue'),
    meta: {
      hidden: true,
    },
    alias: '/:pathMatch(.*)*',
  },
  {
    path: '/:pathMatch(.*)*', // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
    redirect: '/404',
    name: 'ErrorPage',
    meta: {
      hidden: true,
    },
  },
  // 公用菜单路由
  ...LayoutsRouter
];

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = [];

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
});

/** 重置路由 */
export function resetRouter() {
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route;
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name);
      }
    });
  } catch {
    window.location.reload();
  }
}

// 初始化路由守卫
setupAuthGuard(router);

export default router;
